from openai import OpenAI
# 交流机器人
client = OpenAI(
    api_key="sk-on1j51cvbafu31osq7ukpslf3ace04fea1bk7ld3e4g26fjc",
    base_url="https://api.aihao123.cn/luomacode-api/open-api/v1/"
)


def stream_response(prompt):
    response = client.chat.completions.create(
        model="gpt-4",  # 你可以选择其他模型，如"gpt-4"
        messages=[
            {"role": "system", "content": "你是一个翻译助手"},
            {"role": "user", "content": prompt}
        ],
        stream=True
    )

    for chunk in response:
        print(chunk.choices[0].delta.content, end="", flush=True)


# prompt = "Hello怎么翻译"
# stream_response(prompt)

while True:
    user_input = input("你: ")
    if user_input.lower() == '退出':
        print("再见！")
        break
    else:
        print("ai回答:", end="", flush=True)
        stream_response(user_input)
        print()
